package component.auxiliares.view;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import component.auxiliares.Progress;

import control.Supervisorio;
import entities.Variable;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ProgressConfig extends javax.swing.JFrame {
	private JLabel lblText;
	private JTextField txtTexto;
	private JTextField txt0;
	private JButton btnCancel;
	private JButton btnOK;
	private JSeparator jSeparator1;
	private JTextField txt100;
	private JLabel lbl100;
	private JLabel lbl0;
	private JComboBox cmbVariable;
	private JLabel lblVariable;
	private Progress main;

	private ArrayList<Variable> variables;

	private Supervisorio supervisorio;

	/**
	 * Auto-generated main method to display this JFrame
	 */

	public ProgressConfig(Progress j) {
		super();
		main = j;
		supervisorio = Supervisorio.getInstance();
		initGUI();
	}

	private void initGUI() {

		variables = supervisorio.getAllVariables();

		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("i2m - Barra gradual");
			{
				lblText = new JLabel();
				getContentPane().add(lblText);
				lblText.setText("Texto:");
				lblText.setBounds(12, 12, 54, 14);
			}
			{
				txtTexto = new JTextField();
				getContentPane().add(txtTexto);
				txtTexto.setBounds(75, 9, 175, 21);
				txtTexto.setText(main.getPojo().getText());
			}
			{
				lblVariable = new JLabel();
				getContentPane().add(lblVariable);
				lblVariable.setText("Variável:");
				lblVariable.setBounds(12, 38, 82, 14);
			}
			{
				ComboBoxModel cmbVariableModel = new DefaultComboBoxModel(this
						.getVariablesName().toArray());
				cmbVariable = new JComboBox();
				getContentPane().add(cmbVariable);
				cmbVariable.setModel(cmbVariableModel);
				cmbVariable.setBounds(75, 35, 175, 21);
			}
			{
				lbl0 = new JLabel();
				getContentPane().add(lbl0);
				lbl0.setText("0%: ");
				lbl0.setBounds(33, 64, 30, 14);
			}
			{
				txt0 = new JTextField();
				getContentPane().add(txt0);
				txt0.setBounds(75, 61, 56, 21);
				txt0.setText(String.valueOf(main.getPojo().getMin()));
			}
			{
				lbl100 = new JLabel();
				getContentPane().add(lbl100);
				lbl100.setText("100%: ");
				lbl100.setBounds(146, 64, 48, 14);
			}
			{
				txt100 = new JTextField();
				getContentPane().add(txt100);
				txt100.setBounds(194, 61, 56, 21);
				txt100.setText(String.valueOf(main.getPojo().getMax()));
			}
			{
				jSeparator1 = new JSeparator();
				getContentPane().add(jSeparator1);
				jSeparator1.setBounds(12, 94, 238, 10);
			}
			{
				btnOK = new JButton();
				getContentPane().add(btnOK);
				btnOK.setText("OK");
				btnOK.setBounds(12, 104, 110, 21);
				btnOK.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						btnOKMouseClicked(evt);
					}
				});
			}
			{
				btnCancel = new JButton();
				getContentPane().add(btnCancel);
				btnCancel.setText("Cancelar");
				btnCancel.setBounds(139, 104, 112, 21);
				btnCancel.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						btnCancelMouseClicked(evt);
					}
				});
			}

			this.setVariableInput();

			pack();
			this.setSize(270, 166);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setVariableInput() {

		if (main.getPojo().getVariable() != null) {
			for (int i = 0; i < variables.size(); i++) {

				if (variables.get(i).getId() == main.getPojo().getVariable()
						.getId()) {
					cmbVariable.setSelectedIndex(i);
					break;
				}
			}
		}

	}

	public JTextField getTxtTexto() {
		return txtTexto;
	}

	public JTextField getTxt0() {
		return txt0;
	}

	public JTextField getTxt100() {
		return txt100;
	}

	public JComboBox getCmbVariable() {
		return cmbVariable;
	}

	private void btnCancelMouseClicked(MouseEvent evt) {
		this.dispose();
	}

	private void btnOKMouseClicked(MouseEvent evt) {

		int index = cmbVariable.getSelectedIndex();
		Variable v = this.variables.get(index);

		main.getPojo().setVariable(v);
		main.getPojo().setMax(Integer.parseInt(txt100.getText()));
		main.getPojo().setMin(Integer.parseInt(txt0.getText()));
		main.getPojo().setText(txtTexto.getText());

		dispose();

	}

	private ArrayList<String> getVariablesName() {

		ArrayList<String> names = new ArrayList<String>();

		for (Variable variable : variables) {
			names.add(variable.getName());
		}

		return names;

	}
}
